﻿Imports DevExpress.XtraEditors

Public Class frmDanhMuc_TraCuu

    Private Sub frmDanhMuc_TraCuu_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmDanhMuc_TraCuu_Exists = False
    End Sub

    Private Sub frmDanhMuc_TraCuu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Project_Form = "frmMenu" Then
            Me.cmdChon.Visible = False
        End If
        InitGrid()
        LoadGrid()
        frmDanhMuc_TraCuu_Ma = 0
    End Sub

    Private Sub InitGrid()

        Dim i As Integer
        With Me.GridView1

            i = 0
            .Columns(i).FieldName = "chon"
            .Columns(i).Caption = "Chọn"
            .Columns(i).Width = 35
            .Columns(i).OptionsColumn.AllowEdit = True
            If Project_Form = "frmMenu" Then
                .Columns(i).Visible = False
            End If

            i = i + 1
            .Columns(i).FieldName = "mahangmuc"
            .Columns(i).Visible = False

            i = i + 1
            .Columns(i).FieldName = "tenhangmuc"
            .Columns(i).Caption = "Hạng mục"
            .Columns(i).Group()

            i = i + 1
            .Columns(i).FieldName = "ma"
            .Columns(i).Visible = False

            i = i + 1
            .Columns(i).FieldName = "ten"
            .Columns(i).Caption = "Tên vật tư"
            .Columns(i).Width = 170
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "dvt"
            .Columns(i).Caption = "Đvt"
            .Columns(i).Width = 35
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "dgvl"
            .Columns(i).Caption = "Vật liệu"
            .Columns(i).Width = 60
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "dgnc"
            .Columns(i).Caption = "Nhân công"
            .Columns(i).Width = 60
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"
            .Columns(i).OptionsColumn.AllowEdit = False

            i = i + 1
            .Columns(i).FieldName = "dgmtc"
            .Columns(i).Caption = "Máy thi công"
            .Columns(i).Width = 60
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"
            .Columns(i).OptionsColumn.AllowEdit = False

        End With

    End Sub

    Private Sub LoadGrid()
        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable

        If Project_Form = "frmNghiemThu_CapNhat" Or Project_Form = "frmDuToan_CapNhat" Then
            myDtb = myDanhMucF.GetWhereMaHangMuc("01")
        Else
            myDtb = myDanhMucF.GetAll
        End If

        If Project_Form <> "frmMenu" Then

            'xoa bot cac dong da co trong _dtb
            Dim nMa As Decimal
            Dim r As Integer
            For r = 0 To myDtb.Rows.Count - 1
                nMa = myDtb.Rows(r).Item("ma")
                If ExitstValueInDTB(nMa, "ma", frmLoaiChietTinh_Dtb) Then
                    myDtb.Rows(r).Delete()
                End If
            Next
            frmLoaiChietTinh_Dtb.Rows.Clear()

            'them cot chon
            myDtb.Columns.Add("chon", GetType(Boolean))
            Dim k As Integer
            For k = 0 To myDtb.Rows.Count - 1
                myDtb.Rows(k).Item("chon") = False
            Next

        End If

        Me.GridControl1.DataSource = myDtb
        Me.GridView1.ExpandAllGroups()

    End Sub

    Private Sub Search(ByVal sFindWhat As String, ByVal iFindCol As Integer)
        Dim sGridCell As String
        sFindWhat = sFindWhat.ToLower

        Dim i As Integer
        i = 0
        Do While i <= Me.GridView1.DataRowCount - 1
            sGridCell = Me.GridView1.GetDataRow(i).Item(iFindCol).ToString.ToLower

            If sGridCell <> "" And sGridCell.Length >= sFindWhat.Length Then
                If sFindWhat = sGridCell.Substring(0, sFindWhat.Length) Then
                    Exit Do
                End If
            End If
            i = i + 1
        Loop

        Dim r As Integer = 15 'so dong cua grid
        Dim f As Integer = 0 'vi tri cua dong duoc chon

        If i <= Me.GridView1.DataRowCount - 1 Then
            Me.GridView1.FocusedRowHandle = 16

            If i - 5 >= 0 Then
                Me.GridView1.FocusedRowHandle = i - 5
            Else
                Me.GridView1.FocusedRowHandle = 0
            End If

            Me.GridView1.FocusedRowHandle = i
        End If

    End Sub

    Private Sub txtTen_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTen.EditValueChanged
        Search(Me.txtTen.Text, 2)
    End Sub

    Private Sub txtTen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTen.KeyDown
        If e.KeyCode = Keys.Down Then
            Me.GridView1.Focus()
        End If

        If e.KeyCode = Keys.Enter Then
            If Project_Form = "frmLoaiChietTinh" Or Project_Form = "frmChietTinh_CapNhat" Then
                frmDanhMuc_TraCuu_Ma = Me.GridView1.GetDataRow(Me.GridView1.FocusedRowHandle).Item("ma")
                Me.Close()
            End If
        End If

    End Sub

    Private Sub Chon()
        frmDanhMuc_TraCuu_Dtb = New System.Data.DataTable("MyDTB")
        frmDanhMuc_TraCuu_Dtb.Columns.Add("ma", GetType(String))

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetRowCellValue(i, "chon") Then
                frmDanhMuc_TraCuu_Dtb.Rows.Add(New Object() {Me.GridView1.GetRowCellValue(i, "ma")})
            End If
        Next

        Me.Close()
    End Sub

    Private Sub cmdChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChon.Click
        Chon()
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        If Project_Form = "frmLoaiChietTinh" Or Project_Form = "frmChietTinh_CapNhat" Then
            Me.GridView1.SetRowCellValue(Me.GridView1.FocusedRowHandle, "chon", Not Me.GridView1.GetRowCellValue(Me.GridView1.FocusedRowHandle, "chon"))
            Chon()
        End If

    End Sub

    Private Sub GridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridView1.KeyDown
        If e.KeyCode = Keys.Enter Then
            Chon()
        End If
    End Sub

    Private Sub cmdThemmoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemmoi.Click
        frmDanhMuc_CapNhat_AddEdit = "add"
        Dim f As New frmDanhMuc_CapNhat
        f.ShowDialog()

        LoadGrid()


    End Sub

    Private Sub cmdSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSua.Click
        frmDanhMuc_CapNhat_AddEdit = "edit"
        frmDanhMuc_CapNhat_Ma = Me.GridView1.GetDataRow(Me.GridView1.FocusedRowHandle).Item("ma")
        Dim f As New frmDanhMuc_CapNhat
        f.ShowDialog()

        LoadGrid()
    End Sub

    Private Sub cmdXoa_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdXoa.Click
        Dim myLoaiChietTinhCtF As New LoaiChietTinhCtF
        If myLoaiChietTinhCtF.ExistsMa(Me.GridView1.GetDataRow(Me.GridView1.FocusedRowHandle).Item("ma")) Then
            XtraMessageBox.Show("Danh mục đã tồn tại trong loại chiết tính", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim myChietTinhCtF As New ChietTinhCtF
        If myChietTinhCtF.ExistsMa(Me.GridView1.GetDataRow(Me.GridView1.FocusedRowHandle).Item("ma")) Then
            XtraMessageBox.Show("Danh mục đã tồn tại trong chiết tính", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If XtraMessageBox.Show("Chắc chắn danh mục ?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myDanhMucF As New DanhMucF
            If myDanhMucF.Delete(Me.GridView1.GetDataRow(Me.GridView1.FocusedRowHandle).Item("ma")) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
                LoadGrid()
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

End Class